package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.model.record.*;
import com.nuanshui.heatedloan.credit.mongodb.*;
import com.nuanshui.heatedloan.credit.request.CalculationLimitRequest;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/7/22.
 */
@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_ORDER_STATISTICS)
public class OrderStatisticsHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger("OrderStatisticsHandler");

    @Autowired
    private OrderAppRecordRepository orderAppRecordRepository;
    @Autowired
    private OrderContactRecordRepository orderContactRecordRepository;
    @Autowired
    private OrderDeviceInfoRepository orderDeviceInfoRepository;
    @Autowired
    private OrderPhoneRecordRepository orderPhoneRecordRepository;
    @Autowired
    private OrderSmsUserRecordRepository orderSmsUserRecordRepository;


    public void onMessage(Message message, Channel channel ) throws Exception{
          try{
              String msg = new String(message.getBody());
              logger.info("====开始执行订单号与统计数据消息队列,参数为"+msg+"=======");
              CalculationLimitRequest calculationLimitRequest= JSON.parseObject(msg,CalculationLimitRequest.class);
              logger.info("====统计数据与订单号关联数据,大小为客户的id为"+calculationLimitRequest.getCustomerId()+"=======");
              this.dealOrderStatistics(calculationLimitRequest);
              logger.info("===================执行消息队列完成=============");
          }catch(Exception e){
              e.printStackTrace();
          }finally {
              channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
          }
    }


    private void dealOrderStatistics(CalculationLimitRequest calculationLimitRequest){
        //通讯率
        logger.info("=============开始处理通讯录数据=============");
        this.dealContactRecords(calculationLimitRequest);
        logger.info("=========处理通讯录数据完成==================");
        //短信
        logger.info("=============开始处理短信数据=============");
        this.dealSmsRecords(calculationLimitRequest);
        logger.info("=============处理短信数据完成=============");
        //appList
        logger.info("=============开始处理appList数据=============");
        Map<String,Object> orderAppMap=new HashMap<String,Object>();
        this.dealAppRecords(calculationLimitRequest);
        logger.info("=============处理appList数据完成=============");
        //通话记录
        logger.info("=============开始处理通话记录数据=============");
       this.dealPhoneRecords(calculationLimitRequest);
        logger.info("=============处理通话记录数据完成=============");
        //设备信息
        logger.info("=============开始处理设备信息数据=============");
        this.dealDeviceInfo(calculationLimitRequest);
        logger.info("=============处理设备信息数据完成=============");
    }

    //设备信息
    private List<OrderDeviceInfo> dealDeviceInfo(CalculationLimitRequest calculationLimitRequest){
        List<OrderDeviceInfo> orderDeviceInfos=new ArrayList<OrderDeviceInfo>();
        logger.info("=============开始进入处理设备信息数据类=============");
        DeviceRecord deviceInfos=calculationLimitRequest.getDeviceInfo();
        OrderDeviceInfo orderDeviceInfo=new OrderDeviceInfo();
        if(deviceInfos!=null){
            BeanUtils.copyProperties(deviceInfos,orderDeviceInfo);
            orderDeviceInfo.setOrderNo(calculationLimitRequest.getOrderId());
            orderDeviceInfoRepository.save(orderDeviceInfo);
            logger.info("===设备信息处理后的数据为=========="+ orderDeviceInfo.getDeviceId());
        }
        logger.info("=============完成对设备信息数据的处理=============");
        return orderDeviceInfos;
    }
    //电话记录
    private List<OrderPhoneRecord> dealPhoneRecords(CalculationLimitRequest calculationLimitRequest){
        logger.info("=============开始进入处理电话记录数据类=============");
        List<OrderPhoneRecord> orderPhoneRecords=new ArrayList<OrderPhoneRecord>();
        List<PhoneRecord> phoneRecords=calculationLimitRequest.getCallLog();
        OrderPhoneRecord orderPhoneRecord=new  OrderPhoneRecord();
        for(PhoneRecord phoneRecord:phoneRecords){
            BeanUtils.copyProperties(phoneRecord,orderPhoneRecord);
            orderPhoneRecord.setOrderNo(calculationLimitRequest.getOrderId());
            orderPhoneRecords.add(orderPhoneRecord);
            orderPhoneRecordRepository.save(orderPhoneRecord);
        }
        logger.info("=============完成对电话记录数据的处理=============");
        logger.info("===电话记录处理数据的大小为=========="+ phoneRecords.size());
        return orderPhoneRecords;
    }

    private List<OrderAppRecord> dealAppRecords(CalculationLimitRequest calculationLimitRequest){
        logger.info("=============开始进入处理AppList数据处理类=============");
        List<OrderAppRecord> orderAppRecords=new ArrayList<OrderAppRecord>();
        List<AppRecord> appRecords=calculationLimitRequest.getApplist();
        OrderAppRecord orderAppRecord=new OrderAppRecord();
        for(AppRecord appRecord:appRecords){
            BeanUtils.copyProperties(appRecord,orderAppRecord);
            orderAppRecord.setOrderNo(calculationLimitRequest.getOrderId());
            orderAppRecordRepository.save(orderAppRecord);
        }
        logger.info("=============AppList数据处理类数据处理完成=============");
        logger.info("===AppList数据处理类数据处理完成=========="+ appRecords.size());
        return orderAppRecords;
    }

    //短信
    private List<OrderSmsRecord>  dealSmsRecords(CalculationLimitRequest calculationLimitRequest){
        logger.info("=============开始进入处理短信记录数据类=============");
        List<OrderSmsRecord> orderSmsRecords=new ArrayList<OrderSmsRecord>();
        List<SmsRecord>  smsRecords=new ArrayList<SmsRecord>();
        smsRecords=calculationLimitRequest.getMessageList();
        OrderSmsRecord orderSmsRecord=new OrderSmsRecord();
        for(SmsRecord smsRecord:smsRecords){
            BeanUtils.copyProperties(smsRecord,orderSmsRecord);
            orderSmsRecord.setOrderNo(calculationLimitRequest.getOrderId());
            orderSmsRecords.add(orderSmsRecord);
            orderSmsUserRecordRepository.save(orderSmsRecord);
        }
        logger.info("=============短信记录数据类处理完成=============");
        logger.info("===短信记录数据类处理数据的大小为===="+smsRecords.size());
        return orderSmsRecords;
    }
    //通话记录
    private List<OrderContactRecord> dealContactRecords(CalculationLimitRequest calculationLimitRequest){
        logger.info("=============开始进入通讯录数据类=============");
        List<ContactRecord> contactRecords=calculationLimitRequest.getAddressList();
        List<OrderContactRecord> orderContactRecords=new ArrayList<OrderContactRecord>();
        OrderContactRecord orderContactRecord=new OrderContactRecord();
        for(ContactRecord contactRecord:contactRecords){
            BeanUtils.copyProperties(contactRecord,orderContactRecord);
            orderContactRecord.setOrderNo(calculationLimitRequest.getOrderId());
            orderContactRecords.add(orderContactRecord);
            orderContactRecordRepository.save(orderContactRecord);
        }
        logger.info("=============通讯录数据类完成===========");
        logger.info("=====通讯录数据类处理数据的大小为====="+contactRecords.size());
        return orderContactRecords;
    }
}
